package com.workcell.workdata.xthings.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.workcell.workdata.xthings.dto.DictVo;
import com.workcell.workdata.xthings.entity.Dict;
import com.workcell.workdata.xthings.service.DictService;
import com.workcell.workdata.xthings.mapper.DictMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Admin
* @description 针对表【t_dict(字典表)】的数据库操作Service实现
* @createDate 2023-05-17 10:23:19
*/
@Service
@AllArgsConstructor
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict>
    implements DictService{
    private final DictMapper dictMapper;

    @Override
    public List<Dict> getByDictTypeCode(String dictTypeCode) {
        return dictMapper.getByDictTypeCode(dictTypeCode);
    }

    @Override
    public Dict getByDictTypeCodeAndDictCode(String dictTypeCode, String dictCode) {
        LambdaQueryWrapper<Dict> lambdaQueryWrapper = Wrappers.lambdaQuery(Dict.class)
                .eq(Dict::getDictTypeCode, dictTypeCode)
                .eq(Dict::getDictCode, dictCode)
                .last("limit 1");
        return this.getOne(lambdaQueryWrapper);
    }

    @Override
    public boolean save(DictVo dictVo) {
        // 判断字典项编码是否重复
        Dict oldDict = this.getByDictTypeCodeAndDictCode(dictVo.getDictTypeCode(), dictVo.getDictCode());
        Assert.isNull(oldDict, "字典项【{}-{}】已重复", dictVo.getDictTypeCode(), dictVo.getDictCode());
        Dict dict = BeanUtil.copyProperties(dictVo, Dict.class);
        return this.save(dict);
    }
}




